﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：分页控件
//
//创建标识：米海波 || 2011-04-12
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hmc.IM.Common.UserControls
{
    /// <summary>
    /// 分页控件
    /// </summary>
    public partial class ucPaging : UserControl
    {
        /// <summary>
        /// 页面链接集合
        /// </summary>
        private List<LinkLabel> linkList = new List<LinkLabel>();

        /// <summary>
        /// 点击链接页面
        /// </summary>
        public event EventHandler<LinkClickedInfoEventArgs> LinkClickedPage;

        /// <summary>
        /// 上次点击页控件
        /// </summary>
        private LinkLabel OldLink;

        private string _pageInfoMsg = "总记录数：{0}条  总页数：{1}页  ";

        #region 页面属性

        private int _total = 0;
        /// <summary>
        /// 总行数
        /// </summary>
        public int Total
        {
            get { return _total; }
            set { _total = value; }
        }
        private int _pageSize = 100;
        /// <summary>
        /// 页面大小
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }
        private int _pageIndex = 1;
        /// <summary>
        /// 当前页面
        /// </summary>
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }
        private int _totalPage = 1;
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage
        {
            get { return _totalPage; }
            set { _totalPage = value; }
        }

        #endregion

        /// <summary>
        /// 上次访问页号
        /// </summary>
        private int _oldPageIndex = 0;

        /// <summary>
        /// 实例化页面分页控件
        /// </summary>
        public ucPaging()
        {
            InitializeComponent();
            lbPageInfo.Parent = this;
            if (!DesignMode)
            {
                this.Visible = false;
                this.flpRank.Controls.Clear();
                this.BackColor = Color.Transparent;
                flpRank.BackColor = this.BackColor;
                for (int i = 0; i < 12; i++)
                {
                    LinkLabel link = new LinkLabel();
                    link.LinkBehavior = LinkBehavior.NeverUnderline;
                    link.Margin = new Padding(0);
                    link.Name = i.ToString();
                    link.Tag = i;
                    link.AutoSize = true;
                    link.LinkClicked += new LinkLabelLinkClickedEventHandler(link_LinkClicked);
                    if (i == 0 || i == 11)
                    {
                        link.LinkColor = Color.FromArgb(110, 108, 109);
                        link.ActiveLinkColor = Color.FromArgb(251, 89, 4);
                        link.VisitedLinkColor = Color.FromArgb(110, 108, 109);
                        link.Font = new Font("宋体", 7);
                        if (i == 0)
                        {
                            link.Text = "<<";
                        }
                        else
                        {
                            link.Text = ">>";
                        }
                    }
                    else
                    {
                        link.LinkColor = Color.FromArgb(45, 108, 177);
                        link.ActiveLinkColor = Color.FromArgb(251, 89, 4);
                        link.VisitedLinkColor = Color.FromArgb(45, 108, 177);
                        if (this.PageIndex == i)
                        {
                            link.Font = new Font("宋体", 9, FontStyle.Bold);
                            this.OldLink = link;
                        }
                        if (i == 10)
                        {
                            link.Text = "...";
                        }
                        else
                        {
                            link.Text = i.ToString();
                        }

                    }
                    //link.Parent = flpRank;
                    linkList.Insert(i, link);
                }
                for (int i = 11; i >= 0; i--)
                {
                    linkList[i].Parent = flpRank;
                }
                lbPageInfo.Text = string.Format(_pageInfoMsg, this.Total, _totalPage);
                lbPageInfo.Parent = flpRank;
            }
        }


        /// <summary>
        /// 实例化分页控件
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="total">总记录数</param>
        public ucPaging(int pageIndex, int pageSize, int total)
            : this()
        {
            this._pageIndex = pageIndex;
            this._pageSize = pageSize;
            this._total = total;
            this._totalPage = total / pageSize;
            if (total % pageSize != 0)
            {
                this._totalPage++;
            }
            if (this._totalPage == 0)
            {
                this._totalPage++;
            }
        }

        /// <summary>
        /// 按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                LinkLabel currentLink = sender as LinkLabel;
                if (LinkClickedPage != null)
                {
                    int linkIndex = Convert.ToInt32(currentLink.Name);
                    if (linkIndex == 0 || linkIndex == 11)  //点击向左或向右
                    {
                        if (linkIndex == 0)
                        {
                            if (this.PageIndex > 1)
                            {
                                this.PageIndex--;
                                PageOffset(this.PageIndex);
                            }
                        }
                        else
                        {
                            if (this.PageIndex < this.TotalPage)
                            {
                                this.PageIndex++;
                                PageOffset(this.PageIndex);
                            }
                        }
                    }
                    else
                    {
                        this.PageIndex = Convert.ToInt32(currentLink.Tag);
                        PageOffset(PageIndex);
                    }
                    if (this._oldPageIndex != PageIndex)
                    {
                        this._oldPageIndex = PageIndex;
                        LinkClickedPage(sender, new LinkClickedInfoEventArgs(this.PageIndex, this.PageSize, this.Total));
                    }
                }
            }
        }


        /// <summary>
        /// 设置分页控件
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="total">总记录数</param>
        public void SetPaging(int pageIndex, int pageSize, int total)
        {
            this._pageIndex = pageIndex;
            this._pageSize = pageSize;
            this._total = total;
            this._totalPage = total / pageSize;
            //取模值
            int residue = total % pageSize;
            if (residue != 0)
            {
                this._totalPage++;
            }
            //判断总页数是否大于1，如果只有一页则隐藏自身
            if (this._totalPage <= 1)
            {
                this.Visible = false;
            }
            else
            {
                this.Visible = true;
            }
            if (this.TotalPage == 0)
            {
                this._totalPage++;
            }
            lbPageInfo.Text = string.Format(_pageInfoMsg, this.Total, _totalPage);
            PageOffset(this.PageIndex);

        }

        /// <summary>
        /// 偏移加载
        /// </summary>
        /// <param name="index"></param>
        private void PageOffset(int index)
        {
            if (this._oldPageIndex == index)
            {
                return; //如果两次访问同一个页则不执行任何操作
            }
            bool fay = false;   //指示是否有显示不全的页码
            if (this.Total > index + 5)
            {
                fay = true;
            }
            int startNum = 0;
            if (index - 5 < 1)
            {
                startNum = 1;
            }
            else
            {
                startNum = index - 5;
            }
            int maxNum = 0;
            if (index < 5)
            {
                maxNum = 5;
            }
            else
            {
                maxNum = index;
            }
            if (startNum == 1 && maxNum == 5)
            {
                maxNum = 6;
            }
            int j = 1;
            for (int i = startNum; i < maxNum + 5; i++)
            {
                LinkLabel link = linkList[j];
                if (j == 10 && fay)
                    link.Text = "...";
                else
                    link.Text = i.ToString();
                link.Tag = i;
                if (i > this.TotalPage)
                {
                    link.Visible = false;
                }
                else
                {
                    link.Visible = true;
                }
                if (i == index)
                {
                    link.Font = new Font("宋体", 9, FontStyle.Bold);
                }
                else
                {
                    link.Font = new Font("宋体", 9);
                }
                j++;
            }
        }

        private void ucPaging_Load(object sender, EventArgs e)
        {

        }
    }

    /// <summary>
    /// 分页点击事件数据类
    /// </summary>
    public class LinkClickedInfoEventArgs : EventArgs
    {
        private int _total = 0;
        /// <summary>
        /// 总行数
        /// </summary>
        public int Total
        {
            get { return _total; }
            set { _total = value; }
        }
        private int _pageSize = 100;
        /// <summary>
        /// 页面大小
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }
        private int _pageIndex = 1;
        /// <summary>
        /// 当前页面
        /// </summary>
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }
        private int _totalPage;
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage
        {
            get { return _totalPage; }
            set { _totalPage = value; }
        }

        /// <summary>
        /// 事件消息类
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        public LinkClickedInfoEventArgs(int pageIndex, int pageSize, int total)
        {
            this._pageIndex = pageIndex;
            this._pageSize = pageSize;
            this._total = total;
            this._totalPage = total / pageSize;
            if (total % pageSize != 0)
            {
                this._totalPage++;
            }
        }
    }
}
